class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());
        int l = 0, r = people.size() - 1;
        int cnt = 0, sum;
        while (l <= r)
        {
            sum = l == r ? people[l] : people[l] + people[r];
            if (sum <= limit)
                l++;

            r--;
            cnt++;
        }
        return cnt;
    }
};